<html>
<head>

<title>Groovy Goodness: Groovlets as Lightweight Servlets</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Groovlets as Lightweight Servlets</h3>

<div class="post">
<p><a href="http://groovy.codehaus.org/Groovlets">Groovlets</a> are Groovy scripts that are executed by a servlet. With Groovlets a user can request a Groovy script that is executed on the server and the results are displayed in a web browser. We only have to define a servlet in the <code>web.xml</code> of a Java web application, place the Groovy libraries in the web application's <code>lib</code> folder and we can execute Groovy scripts.</p><p>The Groovy script, or we can call it a Groovlet, has a couple of implicit variables we can use. For example <code>reqeust</code>, <code>response</code> to access the <code>HttpServletRequest</code> and <code>HttpServletResponse</code> objects. We have access to the session with the <code>session</code> variable. And if we want to output data we can use <code>out</code>, <code>sout</code> and <code>html</code>.</p>
<p>Let's create a Groovy script <code>serverinfo.groovy</code>:</p>
<pre class="brush:groovy">
def method = request.method

if (!session) {
    session = request.getSession(true)
}

if (!session.groovlet) {
    session.groovlet = 'Groovlets rock!'
}

html.html {
    head {
        title 'Groovlet info'
    }
    body {
        h1 'General info'
        ul {
            li &quot;Method: ${method}&quot;
            li &quot;RequestURI: ${request.requestURI}&quot;
            li &quot;session.groovlet: ${session.groovlet}&quot;
            li &quot;application.version: ${context.version}&quot;
        }
        
        h1 'Headers'
        ul {
            headers.each {
                li &quot;${it.key} = ${it.value}&quot;
            }
        }
    }
}
</pre>
<p>A simple script to start Jetty so we can run our Groovlet:</p>
<pre class="brush:groovy">
import org.mortbay.jetty.Server
import org.mortbay.jetty.servlet.*
import groovy.servlet.*

@Grab(group='org.mortbay.jetty', module='jetty-embedded', version='6.1.14')
def startJetty() {
    def jetty = new Server(9090)
    
    def context = new Context(jetty, '/', Context.SESSIONS)  // Allow sessions.
    context.resourceBase = '.'  // Look in current dir for Groovy scripts.
    context.addServlet(GroovyServlet, '*.groovy')  // All files ending with .groovy will be served.
    context.setAttribute('version', '1.0')  // Set an context attribute.
    
    jetty.start()
}

println &quot;Starting Jetty, press Ctrl+C to stop.&quot;
startJetty()
</pre>
<p>When we run the script to start Jetty and visit http://localhost:9090/serverinfo.groovy with our browser we get the following output:</p>
<a href="http://2.bp.blogspot.com/_-vJw6r2W-bw/St1G01Hjh6I/AAAAAAAAC_8/OEN1GyBUM_0/s1600-h/blog-gg-groovlet.png"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 230px;" src="http://2.bp.blogspot.com/_-vJw6r2W-bw/St1G01Hjh6I/AAAAAAAAC_8/OEN1GyBUM_0/s320/blog-gg-groovlet.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5394545802085631906" /></a
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>